var bearcat = require("bearcat")

var userHandler = function(app) {
	this.app = app;
	this.sessionService = this.app.get('sessionService')
};
//保存单条用户数据
userHandler.prototype.saveUserData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	var key = msg.key
	var value = msg.value
	if(typeof(key) !== "string" || typeof(value) != "string"){
		next(null,{flag : false,msg : "args type error"})
		return
	}
	this.redisDao.db.hset("userData:"+uid,key,value,function(err,data) {
		if(!err){
			next(null,{flag : true})
		}else{
			next(null,{flag : false,msg : err})
		}
	})
}
//批量保存数据
userHandler.prototype.saveBatchUserData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	var datas = msg.datas
	if(!datas || typeof(datas) !== "object"){
		next(null,{flag : false,msg : "args type error"})
		return
	}
	for(var key in datas){
		if(typeof(key) !== "string" || typeof(datas[key]) != "string"){
			next(null,{flag : false,msg : "args type error"})
			return
		}
	}
	this.redisDao.db.hmset("userData:"+uid,datas,function(err,data) {
		if(!err){
			next(null,{flag : true})
		}else{
			next(null,{flag : false,msg : err})
		}
	})
}
//获取单条用户数据
userHandler.prototype.getUserData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	var key = msg.key
	if(typeof(key) !== "string"){
		next(null,{flag : false,msg : "args type error"})
		return
	}
	this.redisDao.db.hget("userData:"+uid,key,function(err,data) {
		if(!err && data){
			next(null,{flag : true,msg : data})
		}else{
			next(null,{flag : false,msg : err})
		}
	})
}
//获取用户全部数据
userHandler.prototype.getUserAllData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	this.redisDao.db.hgetall("userData:"+uid,function(err,data) {
		if(!err && data){
			next(null,{flag : true,msg : data})
		}else{
			next(null,{flag : true,msg : {}})
		}
	})
}
//清除单条用户数据
userHandler.prototype.deleteUserData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	var key = msg.key
	if(typeof(key) !== "string"){
		next(null,{flag : false,msg : "args type error"})
		return
	}
	this.redisDao.db.hdel("userData:"+uid,key,function(err,data) {
		if(!err){
			next(null,{flag : true})
		}else{
			next(null,{flag : false,msg : err})
		}
	})
}
//清除全部用户数据
userHandler.prototype.deleteAllUserData = function(msg, session, next) {
	var uid = session.get("uid")
	if(!uid){
		next(null,{flag : false,msg : "未登录"})
		return
	}
	this.redisDao.db.del("userData:"+uid,function(err,data) {
		if(!err){
			next(null,{flag : true})
		}else{
			next(null,{flag : false,msg : err})
		}
	})
}
module.exports = function(app) {
  return bearcat.getBean({
  	id : "userHandler",
  	func : userHandler,
  	args : [{
  		name : "app",
  		value : app
  	}],
  	props : [{
  		name : "redisDao",
  		ref : "redisDao"
  	}]
  })
};